package com.ftx.controller.test.annotation.condition;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Created by fengtingxin on 2020/4/10.
 */
//判断是否为windows系统
public class WindowsCondition implements Condition {

    /**
     *
     * @param context 判断条件能够只用的上下文环境
     * @param metadata 注释信息
     * @return
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        System.out.println("我再 WindowsCondition");
        // 1.能够获取到ioc使用的beanFactory
        BeanFactory beanFactory = context.getBeanFactory();
        // 2. 获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        // 3. 获取环境
        Environment environment = context.getEnvironment();
        // 4. 获取bean定义的注册类
        BeanDefinitionRegistry beanDefinition = context.getRegistry();

        String systemName = environment.getProperty("os.name");
        if (systemName.contains("Windows")){
            System.out.println("os.name 包含windows");
            return true;
        }
        return false;
    }
}
